<?php
header("Content-Type:text/html;charset=utf-8");
include_once 'common/common.function.php';
include_once 'common/database.class.php';
if(!empty($_POST['submit'])){	
	/** 建立数据库连接 **/
	$db = new DataBase();	
	/** 创建版本 **/
	$row = $db->get_one("select * from yabb_sys_ver order by Id desc limit 1");
	$verno = intval($row['Id']) + 1;	
	$vertime = intval($row['CreateTime']);
	$vername = $_POST['version_name'];
	$verdesc = $_POST['version_desc'];
	$time = time();
	$option = 1;
	$status = 0;
	$sql = "insert into yabb_sys_ver 
	(`Id`,`VerNo`,`VerName`,`VerDesc`,`CreateTime`,`SyncOption`,`Status`)
	 values  
	({$verno},{$verno},'{$vername}','{$verdesc}',{$time},{$option},{$status})";
	$db->execute($sql);	
	/** 创建版本 **/
	
	/** 取得版本文件 **/
	$dirname = str_replace('\\','/',dirname(__FILE__)).'/';	
	$dir = $dirname.'tmp';
	$temp_file_list = read_dir($dir);
	foreach($temp_file_list as $key=>$val){
		$filename = $val['path'].'/'.$val['file'];
		$new_path = str_replace($dirname,'',str_replace('\\','/',$val['path']));
		$row = $db->get_one(" select Md5,FileSize from yabb_sys_ver_file where FilePath='".$new_path."' and FileName='".$val['file']."' order by VerNo desc limit 1");
		$file_md5 = md5(file_get_contents($filename));
		if($row && $row['Md5']==$file_md5){
			unset($temp_file_list[$key]);
			continue;
		}
		$temp_file_list[$key]['path'] = $new_path;
	}
	$file_list = array_merge($temp_file_list);
	/** 取得版本文件 **/
	
	/** 写入版本文件 **/
	$todir = $dirname.'ver/'.$verno.'/';
	foreach($file_list as $key=>$val){
		$filepath = $val['path'];
		$filename = $dirname.$filepath.'/'.$val['file'];	
		$filesize = filesize($filename);
		$filecontent = file_get_contents($filename);
		$filemd5 = md5($filecontent);
		$sql = "insert into yabb_sys_ver_file 
		(`Id`,`VerNo`,`FileName`,`FilePath`,`FileSize`,`Md5`) values 
		(NULL,{$verno},'{$val['file']}','{$filepath}',{$filesize},'{$filemd5}') 
		";		
		$db->execute($sql);
		/** 复制文件 **/
		$desfile = $todir.$filepath.'/'.$val['file'];
		$desdir  = dirname($desfile);
		if(!is_dir($desdir)){
			create_dir($desdir);
		}
		file_put_contents($desfile, $filecontent);		
	}
	/** 写入版本文件 **/
	
	
	/** 数据库版本 **/
	$all_table = $table_info = array();
	$rows = $db->get_all("show tables");
	foreach($rows as $tmp){
		foreach($tmp as $table){
			$all_table[] = $table;
		}
	}	
	foreach($all_table as $table){		
		$craete_table = $db->get_one(" show create table ".$table);
		$table_info[$table]['create'] = $craete_table['Create Table'];
		
		$fields = $db->get_one(" show fields from ".$table);
		$table_info[$table]['fields'] = $fields;
		
		$indexs = $db->get_all(" show index from ".$table);
		$new_index = array();
		foreach ($indexs as $k=>$v){
			$KeyName=$v['Key_name'];
			if(isset($new_index[$KeyName])){
				$new_index[$KeyName]['Column'].=','.$v['Column_name'];
				$new_index[$KeyName]['IsAutoInc']=0;//复合索引不可能有自增字段
			}else{
				$new_index[$KeyName]['Column']=$v['Column_name'];
				$new_index[$KeyName]['NoUnique']=intval($v['Non_unique']);
				$new_index[$KeyName]['IsAutoInc']=$OriFields[$v['Column_name']]['Extra']=='auto_increment'?1:0;
			}
		}
		$table_info[$table]['index'] = $new_index;
	}
	$sql_base_file = '__DBSQL_'.$verno.'.php';
	$sql_file = $todir.'sql/'.$sql_base_file;
	$sql_dir = dirname($sql_file);
	if(!is_dir($sql_dir)){
		create_dir($sql_dir);
	}
	$s = gzcompress(serialize($table_info));
	file_put_contents($sql_file, $s);
	
	$filepath = 'sql';
	$filesize = strlen($s);
	$filemd5 = md5($s);
	$sql = "insert into yabb_sys_ver_file 
	(`Id`,`VerNo`,`FileName`,`FilePath`,`FileSize`,`Md5`) values 
	(NULL,{$verno},'{$sql_base_file}','{$filepath}',{$filesize},'{$filemd5}') 
	";		
	$db->execute($sql);
	/** 数据库版本 **/
	
	/** 写入完成更改版本状态  **/
	$sql = " update yabb_sys_ver set Status='1' where Id='{$verno}' ";
	$db->execute($sql);
	echo '版本创建完成<br>';
	echo '<a href="form.php">返回</a>';
}else{
?>
<script>
function check_form(){
	var version_name = document.getElementById('version_name').value;
	var version_desc = document.getElementById('version_desc').value;
	if(version_name=="" || version_content==""){
		alert('not empty');
		return false;
	}
	return true;
}
</script>
<form method="post" >
<table>
<tr>
<td align="right">version name:</td>
<td ><input type="text" name="version_name"></td>
</tr>
<tr>
<td align="right">version conttent:</td>
<td ><input type="text" name="version_desc"></td>
</tr>
<tr>
<td align="right"></td>
<td >
<input type="submit" name="submit" value="add" onclick="return check_form();"/>&nbsp;&nbsp;
<input type="reset" name="reset" value="reset"/>
</td>
</tr>
</table>
</form>
<?php }?>